package com.suxin.lock;

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.LockSupport;

/**
 * @author Tang
 * @classname MyLockQueue
 * @description [  ]
 * @date 2021/3/17 11:14
 */
public class MyLockQueue {

    private ConcurrentLinkedQueue<Thread> queue = new ConcurrentLinkedQueue<>();
    private AtomicBoolean atomicBoolean = new AtomicBoolean(false);

    public void lock() {
        Thread currentThread = Thread.currentThread();
        queue.add(currentThread);
        while (queue.peek() != currentThread && !atomicBoolean.compareAndSet(false,true)) {
            LockSupport.park(currentThread);
        }
        queue.remove();
    }

    public void unLock() {
        atomicBoolean.compareAndSet(true, false);
        LockSupport.unpark(queue.peek());
    }

    public static void main(String[] args) {

    }

}